package com.itboye.ihomebank.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ChanKanWeiXiuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.me.ActivityChaKanShiFu;
import com.itboye.ihomebank.activity.me.ActivityXiuGaiMoney;
import com.itboye.ihomebank.activity.guanjia.ActivityXiuLiZhiFu;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 我的生活管家（维修中）
 * 
 */
public class MyGuanJiaAdapter02 extends MutiplyCommonAdapter<ChanKanWeiXiuBean>
		implements Observer {
	List<ChanKanWeiXiuBean> datas;
	Context context;

	RepairPersenter repairPersenter;
	int position;
	String id;
	protected String money;
	protected String repairStatus;
	protected String uid;

	public MyGuanJiaAdapter02(Context context, List<ChanKanWeiXiuBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		this.datas = datas;
		this.context = context;
		repairPersenter = new RepairPersenter(MyGuanJiaAdapter02.this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		this.position = position;
	}

	@Override
	public void convert(ViewHolder holder, final ChanKanWeiXiuBean t, int type,
			int position) {
		holder.setText(R.id.guanjia_danhao, t.getId());
		holder.setText(R.id.guanjia_leixing, t.getRepairstatus());
		holder.setText(R.id.guanjia_address, t.getAddress());
		holder.setText(R.id.guanjia_name, t.getUname());

		holder.setText(R.id.guanjia_01, "查看师傅");
		holder.setText(R.id.guanjia_02, "修理完成");
		holder.setText(R.id.guanjia_03, "修改金额");

		// 查看师傅
		holder.setOnClickListener(R.id.guanjia_01, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context,
								ActivityChaKanShiFu.class);
						intent.putExtra("head", t.getHead());
						intent.putExtra("name", t.getWname());
						intent.putExtra("phone", t.getWmobile());
						intent.putExtra("id", t.getWid());
						context.startActivity(intent);
					}
				});
		// 修理完成
		holder.setOnClickListener(R.id.guanjia_02, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// if(Integer.parseInt(t.getMoney())<=0){
						id = t.getId();
						money = t.getMoney();
						repairStatus = t.getRepairstatus();
						repairPersenter.xiuLiEnd(t.getId(), uid);
						// }else{

						// }
					}
				});
		// 修改金额
		holder.setOnClickListener(R.id.guanjia_03, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context,
								ActivityXiuGaiMoney.class);
						intent.putExtra("id", t.getId());
						intent.putExtra("mobile", t.getWmobile());
						context.startActivity(intent);
					}
				});

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			if (!entity.getCode().equals(0)) {

				if (entity.getEventType() == RepairPersenter.theEnd_success) {
					ByAlert.alert(entity.getData());

					Iterator<ChanKanWeiXiuBean> iterator = datas.iterator();
					while (iterator.hasNext()) {
						ChanKanWeiXiuBean bean = iterator.next();
						if (id.equals(bean.getId())) {
							iterator.remove();
							notifyDataSetChanged();
							break;
						}
					}
					Intent intent = new Intent(context,
							ActivityXiuLiZhiFu.class);
					intent.putExtra("id", id);
					intent.putExtra("money", money);
					intent.putExtra("type", repairStatus);
					context.startActivity(intent);
				}
				if (entity.getEventType() == RepairPersenter.theEnd_fail) {
					ByAlert.alert(entity.getData());
				}
			}
		}
	}

}
